package com.demo.resourcecode.c11mvc.asyncRestTemplate;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.Netty4ClientHttpRequestFactory;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.client.AsyncRestTemplate;

public class SampleTests {

    private static final int DEFAULT_SLEEP_MILLIS = 20;
    private static final Logger logger = LoggerFactory.getLogger(SampleTests.class);

	public static void main(String[] args) throws InterruptedException {
		AsyncRestTemplate restTemplate = new AsyncRestTemplate(new Netty4ClientHttpRequestFactory());
		for(int i=0; i<1; i++) {
			// 《springboot2.x》-《com.spring.demo.c9mvc》
			String URL = "http://127.0.0.1:8081/test/" + i;
	        ListenableFuture<ResponseEntity<String>> listenableFuture = restTemplate.getForEntity(URL, String.class);
	        listenableFuture.addCallback(result -> logger.info("result = " + result.getBody()), Throwable::printStackTrace);
	        logger.info("executor end");
	        while (!listenableFuture.isDone()) {
	            Thread.sleep(DEFAULT_SLEEP_MILLIS);
	        }
	        Thread.sleep(DEFAULT_SLEEP_MILLIS);
	        logger.info("the end");
		}
	}

}
